﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;

public partial class usercontrols_RecentViewItem : System.Web.UI.Page
{
    List<Item> li = new List<Item>();
    Hachi.Controllers.ProductsController pctr = new Hachi.Controllers.ProductsController();
    protected void Page_Load(object sender, EventArgs e)
    {
        string cck = hachiCookies.getCookieValue("recentview");
        try
        {
            if (Session["recentview"] != null)
                li = (List<Item>)(Session["recentview"]);
            else if (!string.IsNullOrEmpty(cck))
            {
                string[] i = cck.Split('-');
                li = new List<Item>();
                for (int j = 1; j < i.Count(); j++)
                {
                    string[] d = i[j - 1].Split(':');
                    Item it = new Item();
                    it.ItemID = int.Parse(d[0]);
                    it.Quantity = int.Parse(d[1]);
                    li.Add(it);
                }

            }



            if (Request.QueryString["cmd"] != null && Request.QueryString["cmd"] == "getAllItemInrecentview")
            {
                getItemInrecentview(999);
            }
            else if (Request.QueryString["cmd"] != null && Request.QueryString["cmd"] == "getItemInrecentview")
            {
                getItemInrecentview();
            }
            else if (Request.QueryString["cmd"] != null && Request.QueryString["cmd"] == "count")
            {
                WriteSumQuantity();
            }
            else if (Request.QueryString["cmd"] != null && Request.QueryString["cmd"] == "del")
            {
                int id = int.Parse(Request.QueryString["pid"]);
                Item i = li.Where(d => d.ItemID == id).FirstOrDefault();
                if (i != null)
                {
                    li.Remove(i);
                    save();
                }
                getItemInrecentview();
            }
            else if (Request.QueryString["cmd"] != null && Request.QueryString["cmd"] == "delall")
            {
                li = new List<Item>();
                save();
            }
            else
            {
                int id = int.Parse(Request.QueryString["pid"]);
                Item i = li.Where(d => d.ItemID == id).FirstOrDefault();
                if (i == null)
                {
                    i = new Item(); i.ItemID = id; li.Insert(0,i);
                    save();
                }
                getItemInrecentview();

            }
        }
        catch (Exception)
        {
            Response.Write("fail");
        }

    }
    void save()
    {
        Session["recentview"] = li;
        string ck = "";
        foreach (var item in li)
            ck += item.ItemID + ":" + item.Quantity + "-";
        hachiCookies.setCookieValue("recentview", ck);
    }
    void WriteSumQuantity()
    {
        Response.Write(li.Count);
    }
    private void getItemInrecentview()
    {
        getItemInrecentview(6);
    }
    private void getItemInrecentview(int take)
    {
        
        
        string Lang = Session["Lang"] == null ? hachiCookies.getCookieValue("Lang") : Session["Lang"].ToString();
        JavaScriptSerializer js = new JavaScriptSerializer();
        var re = li.Take(take).Select(s =>
            new
            {
                ProductID = s.ItemID,
                pctr.getProduct(s.ItemID).DisplayCode,
                OrderQuantity = s.Quantity,
                pctr.getProduct(s.ItemID).Quantity,
                Price = String.Format("{0:#,#.# vnđ}", pctr.getProduct(s.ItemID).Price),
                IsSaleOff = pctr.IsPromotionProduct(s.ItemID),
                IsNew = pctr.IsInType(s.ItemID, 5),
                PriceSaleOff = String.Format("{0:#,#.# vnđ}", pctr.getProduct(s.ItemID).PriceSaleOff),
                pctr.getProduct(s.ItemID).Hit,
                pctr.getProductDetailToShow(pctr.getProduct(s.ItemID), Lang).ProductName,
                pctr.getProductDetailToShow(pctr.getProduct(s.ItemID), Lang).Alias,
                IsRecommended = pctr.IsInType(s.ItemID, 3),
                IsUnique = pctr.IsInType(s.ItemID, 2),
                IsTopical = pctr.IsTopicalProduct(s.ItemID)
            });
        string strJSON = js.Serialize(
            re
            );
        Response.Write(strJSON);
    }
    
}